//
//  ViewController.m
//  CoreData
//
//  Created by dllo on 16/3/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Student.h"
#import "CoreDataManager.h"

@interface ViewController ()

@property (nonatomic, strong)CoreDataManager *coreDataManager;

@property (nonatomic, strong)NSMutableArray *arrayOfObjects;
@end

@implementation ViewController

/** 添加 */
- (IBAction)tianjia:(id)sender {
    NSLog(@"%@", [self.coreDataManager applicationDocumentsDirectory]);
    
    Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    
    stu.name = @"张三";
    stu.age = @"220";
    stu.sex = @"m";
    stu.hobby = @"女";
    
    Student *stu1 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    
    stu1.name = @"李四";
    stu1.age = @"2240";
    stu1.sex = @"m";
    stu1.hobby = @"男";

    Student *stu2 = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    
    stu2.name = @"王五";
    stu2.age = @"224770";
    stu2.sex = @"w";
    stu2.hobby = @"中";
    
    [self.coreDataManager saveContext];
    
}

/** 删除 */
- (IBAction)shanchu:(id)sender {
    
    Student *stu = [self.arrayOfObjects firstObject];
    [self.coreDataManager.managedObjectContext deleteObject:stu];
    [self.coreDataManager saveContext];
    
}

/** 修改 */
- (IBAction)xiugai:(id)sender {
    
    Student *stu = [self.arrayOfObjects firstObject];
    stu.name = @"我靠";
    
    [self.coreDataManager saveContext];
}

/** 查询 */
- (IBAction)chaxun:(id)sender {
    NSLog(@"%@", [self.coreDataManager applicationDocumentsDirectory]);

//    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.coreDataManager.managedObjectContext];
//    [fetchRequest setEntity:entity];
//   
//    NSError *error = nil;
//    self.arrayOfObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error].mutableCopy;
//    if (_arrayOfObjects == nil) {
//        NSLog(@"%@", error);
//    }

    
    /** 1. age < %d */
    /** 2. name以张开头的  name like '张*' */
    /** 3. name中带"四"的  name like '*四*' */
    /** 4. name中带四的, 但不以四开头的  name like '?四*'  */
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.coreDataManager.managedObjectContext];
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '*四*'"];
    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    self.arrayOfObjects = [self.coreDataManager.managedObjectContext executeFetchRequest:fetchRequest error:&error].mutableCopy;
    if (_arrayOfObjects == nil) {
        
        NSLog(@"%@", error);
    }
    
    for (Student *stu in self.arrayOfObjects) {
        NSLog(@"%@", stu.name);
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.coreDataManager = [CoreDataManager shareCoreDataManager];
}



































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
